package com.hd123.rumba.gwt.widget2e.client.form;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hd123.rumba.gwt.base.client.BWithUCN;
import com.hd123.rumba.gwt.widget2e.client.dialog.CodeNameFilter;
import com.hd123.rumba.gwt.widget2e.client.dialog.RSimpleBrowserDialog;

/**
 * 使用了{@link RSimpleBrowserDialog}的UCNBox
 * 
 * @author LiQi
 * 
 */
public abstract class RSimpleUCNBox extends RUCNBox {

  public RSimpleUCNBox() {
    super(RSimpleBrowserDialog.COLUMN_CAPTIONS);
    setUCNCallback(new UCNCallback());
  }

  protected void buildBrowserDialog() {
    browser = new Browser();
  }

  /**
   * 需要根据用户输入的代码查询BWithUCN对象时触发。实现类应在本方法的实现中执行RPC调用，
   * 把code传给servlet，返回查询结果，注意返回的对象必须是BWithUCN。
   * 
   * @param code
   *          用户输入的代码
   * @param callback
   *          实现类应把callback直接作为自己RPC调用的callback
   */
  public abstract void queryByCode(String code, AsyncCallback callback);

  /**
   * 执行RPC调用获得数据集，数据集应为RPageData类型，数据集的元素应为BWithUCN类型
   * 
   * @param filter
   *          筛选对象，包含当前页序号、每页记录数、筛选数据、排序数据
   * @param callback
   *          实现类应把callback作为RPC调用的回调
   */
  public abstract void fetchBrowserData(CodeNameFilter filter, AsyncCallback callback);

  /**
   * UCN回调
   * 
   * @author LiQi
   * 
   */
  private class UCNCallback implements RUCNCallback {

    public String displayBrowserData(Object data, int col) {
      return null;
    }

    public void fetchBrowserData(int page, int pageSize, String sortField, String sortDir,
        AsyncCallback callback) {
    }

    public void queryByCode(String code, AsyncCallback callback) {
      RSimpleUCNBox.this.queryByCode(code, callback);
    }
  }

  /**
   * 浏览对话框
   * 
   * @author LiQi
   * 
   */
  private class Browser extends RSimpleBrowserDialog {

    public void fetchData(CodeNameFilter filter, AsyncCallback callback) {
      RSimpleUCNBox.this.fetchBrowserData(filter, callback);
    }

    public void onSelected(BWithUCN object) {
      onBrowserReturn(object);
    }
  }
}
